﻿var lang = [];
var thisLanguages = "manage-zh-CN";
var topElement;

function getLanguages(layui, language, pageType) {
    var $ = layui.$;
    thisLanguages = language;
    $.getJSON("/language/" + language + ".json", function (langs) {
        lang = langs;
        if (!pageType) {
            SetPageLanguage(layui, language);
        } else {
            SetFormPageLanguage(layui);
        }
    });
}

function timestamp(s) {
    return Math.round(new Date(s));
}
function untimestamp(s) {
    if (s === null) {
        return "";
    }
    return formatTime(s, 'Y-M-D h:m:s');
}
function ymd(s) {
    return formatTime(s, 'Y-M-D');
}
// 格式化日期，如月、日、时、分、秒保证为2位数
function formatNumber(n) {
    n = n.toString();
    return n[1] ? n : '0' + n;
}
// 参数number为毫秒时间戳，format为需要转换成的日期格式
function formatTime(number, format) {
    let time = new Date(number);
    let newArr = [];
    let formatArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    newArr.push(time.getFullYear());
    newArr.push(formatNumber(time.getMonth() + 1));
    newArr.push(formatNumber(time.getDate()));

    newArr.push(formatNumber(time.getHours()));
    newArr.push(formatNumber(time.getMinutes()));
    newArr.push(formatNumber(time.getSeconds()));

    for (let i in newArr) {
        format = format.replace(formatArr[i], newArr[i]);
    }
    return format;
}

// 参数number为毫秒时间戳，format为需要转换成的日期格式
function formatTimeymd(number, format) {
    let time = new Date(number);
    let newArr = [];
    let formatArr = ['Y', 'M', 'D'];
    newArr.push(time.getFullYear());
    newArr.push(formatNumber(time.getMonth() + 1));
    newArr.push(formatNumber(time.getDate()));

    for (let i in newArr) {
        format = format.replace(formatArr[i], newArr[i]);
    }
    return format;
}

function encodeUnicode(str) {
    return str;
}
function decodeUnicode(str) {
    return decodeURI(str);
}

function GetData(s) {
    var str = "";
    var date = new Date();
    console.log(date);
    switch (s) {
        case 1:
            str = date.getFullYear() + '-' + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
            break;
        case 2:
            str = date.getFullYear() + '-' + (date.getMonth() + 1) + "-" + date.getDate();
            break;
        default:
            break;
    }

    return str;
}

function dateTest() {
    var date = new Date();

    console.log(date.getYear()); //获取当前年份(2位)

    console.log(date.getFullYear()); //获取完整的年份(4位)

    console.log(date.getMonth()); //获取当前月份(0-11,0代表1月)

    console.log(date.getDate()); //获取当前日(1-31)

    console.log(date.getDay()); //获取当前星期X(0-6,0代表星期天)

    console.log(date.getTime()); //获取当前时间(从1970.1.1开始的毫秒数)

    console.log(date.getHours()); //获取当前小时数(0-23)

    console.log(date.getMinutes()); //获取当前分钟数(0-59)

    console.log(date.getSeconds()); //获取当前秒数(0-59)

    console.log(date.getMilliseconds()); //获取当前毫秒数(0-999)

    console.log(date.toLocaleDateString()); //获取当前日期

    var mytime = date.toLocaleTimeString(); //获取当前时间

    console.log(date.toLocaleString()); //获取日期与时间
}

/** 
* 获取本周、本季度、本月、上月的开端日期、停止日期 
*/
var now = new Date(); //当前日期 
var nowDayOfWeek = now.getDay(); //今天本周的第几天 
var nowDay = now.getDate(); //当前日 
var nowMonth = now.getMonth(); //当前月 
var nowYear = now.getYear(); //当前年 
nowYear += (nowYear < 2000) ? 1900 : 0; // 

var lastMonthDate = new Date(); //上月日期 
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
var lastYear = lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth();

//格局化日期：yyyy-MM-dd 
function formatDate(date) {
    var myyear = date.getFullYear();
    var mymonth = date.getMonth() + 1;
    var myweekday = date.getDate();

    if (mymonth < 10) {
        mymonth = "0" + mymonth;
    }
    if (myweekday < 10) {
        myweekday = "0" + myweekday;
    }
    return (myyear + "-" + mymonth + "-" + myweekday);
}

//获得某月的天数 
function getMonthDays(myMonth) {
    var monthStartDate = new Date(nowYear, myMonth, 1);
    var monthEndDate = new Date(nowYear, myMonth + 1, 1);
    var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
}

//获得本季度的开端月份 
function getQuarterStartMonth() {
    var quarterStartMonth = 0;
    if (nowMonth < 3) {
        quarterStartMonth = 0;
    }
    if (2 < nowMonth && nowMonth < 6) {
        quarterStartMonth = 3;
    }
    if (5 < nowMonth && nowMonth < 9) {
        quarterStartMonth = 6;
    }
    if (nowMonth > 8) {
        quarterStartMonth = 9;
    }
    return quarterStartMonth;
}

//获得本周的开端日期 
function getWeekStartDate() {
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
    return formatDate(weekStartDate);
}

//获得本周的停止日期 
function getWeekEndDate() {
    var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
    return formatDate(weekEndDate);
}

//获得本月的开端日期 
function getMonthStartDate() {
    var monthStartDate = new Date(nowYear, nowMonth, 1);
    return formatDate(monthStartDate);
}

//获得本月的停止日期 
function getMonthEndDate() {
    var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
    return formatDate(monthEndDate);
}

//获得上月开端时候 
function getLastMonthStartDate() {
    var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
    return formatDate(lastMonthStartDate);
}

//获得上月停止时候 
function getLastMonthEndDate() {
    var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
    return formatDate(lastMonthEndDate);
}

//获得本季度的开端日期 
function getQuarterStartDate() {

    var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
    return formatDate(quarterStartDate);
}

//或的本季度的停止日期 
function getQuarterEndDate() {
    var quarterEndMonth = getQuarterStartMonth() + 2;
    var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
    return formatDate(quarterStartDate);
}

//获得本年开始
function getStartYearDate() {
    var quarterStartDate = new Date(nowYear, 0, 1);
    return formatDate(quarterStartDate);
}

//或的本年结束 
function getEndStartDate() {
    var quarterStartDate = new Date(nowYear, 11, 31);
    return formatDate(quarterStartDate);
}

function toDecimal2(x) {
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}

function SetPageLanguage(layui) {
    var $ = layui.$;
    var flexible = document.getElementById("flexible");
    var refresh = document.getElementById("refresh");
    var UserName = document.getElementById("userName");
    var email = document.getElementById("email");
    var UserPwd = document.getElementById("userPwd");
    var validCode = document.getElementById("validCode");
    var loginButton = document.getElementById("login-submit");
    var remember = document.getElementById("remember");
    if (remember) {
        remember.setAttribute('title', lang.remember);
        layui.form.render();
    }
    if (flexible) {
        flexible.setAttribute('title', lang.flexible);
    }
    if (refresh) {
        refresh.setAttribute('title', lang.refresh);
    }
    if (email) {
        email.placeholder = lang.email;
    }
    if (UserName) {
        UserName.placeholder = lang.userName;
    }
    if (UserPwd) {
        UserPwd.placeholder = lang.userPwd;
    }
    if (validCode) {
        validCode.placeholder = lang.validCode;
    }
    if (loginButton) {
        loginButton.innerText = lang.loginButtonText;
    }

    //设置所有菜单显示文本
    $("*").each(function () {
        var controlId = $(this).attr("id");
        if (controlId) {
            var layLevel = $(this).attr("lay-level");
            if (layLevel === "two") {
                for (var keys in lang) {
                    if (lang.hasOwnProperty(keys)) {
                        if (keys === controlId) {
                            $(this).find("cite").text(lang[keys]);
                        }
                    }
                }
            } else {
                var hasNestedTags = false;
                if ($(this).find('i').length === 0) {
                    hasNestedTags = true;
                }
                var controlModel = document.getElementById(controlId);
                if (hasNestedTags) {
                    for (var key in lang) {
                        if (lang.hasOwnProperty(key)) {
                            if (key === controlId) {
                                if ($(controlModel).attr("lay-level") === "two") {
                                    continue;
                                }
                                controlModel.innerText = lang[key];
                            }
                        }
                    }
                } else {
                    for (var keys in lang) {
                        if (lang.hasOwnProperty(keys)) {
                            if (keys === controlId) {
                                controlModel.setAttribute('lay-tips', lang[keys]);
                                $(this).find("cite").text(lang[keys]);
                            }
                        }
                    }
                }
            }
        }
    });
}

/*设置列表页语言 */
function SetListPageLanguage(layui) {
    var $ = layui.$;
    //设置所有菜单显示文本
    var controlId;
    $("*").each(function () {
        switch ($(this)[0].tagName) {
            case "TH":
                controlId = $(this).attr("data-field");
                if (controlId) {
                    for (var key in lang) {
                        if (lang.hasOwnProperty(key)) {
                            if (key === controlId) {
                                $(this).attr("title", lang[key]);
                                var spanIndex = 0;
                                $(this).find("span").each(function () {
                                    if (spanIndex === 0) {
                                        $(this).text(lang[key]);
                                    }
                                    spanIndex++;
                                });
                            }
                        }
                    }
                }
                break;
            case "INPUT":
                var inputType = $(this).attr("type");
                controlId = $(this).attr("placeholder");
                if (inputType === "radio" || inputType === "checkbox") {
                    if (controlId) {
                        for (var key in lang) {
                            if (lang.hasOwnProperty(key)) {
                                if (key === controlId) {
                                    $(this).attr("title", lang[key]);
                                    layui.form.render();
                                }
                            }
                        }
                    } else {
                        controlId = $(this).attr("layui-value");
                        if (controlId) {
                            for (var key in lang) {
                                if (lang.hasOwnProperty(key)) {
                                    if (key === controlId) {
                                        $(this).attr("title", lang[key]);
                                        layui.form.render();
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (controlId) {
                        for (var key in lang) {
                            if (lang.hasOwnProperty(key)) {
                                if (key === controlId) {
                                    $(this).attr("placeholder", lang[key]);
                                }
                            }
                        }
                    } else {
                        controlId = $(this).attr("layui-value");
                        if (controlId) {
                            for (var key in lang) {
                                if (lang.hasOwnProperty(key)) {
                                    if (key === controlId) {
                                        $(this).val(lang[key]);
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            default:
                controlId = $(this).attr("lay-event");
                if (controlId) {
                    for (var key in lang) {
                        if (lang.hasOwnProperty(key)) {
                            if (key === controlId) {
                                $(this).text(lang[key]);
                            }
                        }
                    }
                }
                break;
        }
    });
}
/*设置表单页语言 */
function SetFormPageLanguage(layui) {
    var $ = layui.$;
    //设置所有菜单显示文本
    var controlId;
    $("*").each(function () {
        switch ($(this)[0].tagName) {
            case "INPUT":
                var inputType = $(this).attr("type");
                controlId = $(this).attr("placeholder");
                if (inputType === "radio" || inputType === "checkbox") {
                    if (controlId) {
                        for (var key in lang) {
                            if (lang.hasOwnProperty(key)) {
                                if (key === controlId) {
                                    $(this).attr("title", lang[key]);
                                    layui.form.render();
                                }
                            }
                        }
                    } else {
                        controlId = $(this).attr("lay-event");
                        if (controlId) {
                            for (var key in lang) {
                                if (lang.hasOwnProperty(key)) {
                                    if (key === controlId) {
                                        $(this).attr("title", lang[key]);
                                        layui.form.render();
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (controlId) {
                        for (var key in lang) {
                            if (lang.hasOwnProperty(key)) {
                                if (key === controlId) {
                                    $(this).attr("placeholder", lang[key]);
                                }
                            }
                        }
                    }
                }
                break;
            case "TEXTAREA":
                controlId = $(this).attr("placeholder");
                if (controlId) {
                    for (var key in lang) {
                        if (lang.hasOwnProperty(key)) {
                            if (key === controlId) {
                                $(this).attr("placeholder", lang[key]);
                            }
                        }
                    }
                }
                break;
            case "BUTTON":
                controlId = $(this).attr("id");
                if (controlId) {
                    for (var key in lang) {
                        if (lang.hasOwnProperty(key)) {
                            if (key === controlId) {
                                $(this).text(lang[key]);
                            }
                        }
                    }
                }
                break;
            default:
                controlId = $(this).attr("lay-event");
                if (controlId) {
                    for (var key in lang) {
                        if (lang.hasOwnProperty(key)) {
                            if (key === controlId) {
                                $(this).text(lang[key]);
                            }
                        }
                    }
                }
                break;
        }
    });
}

/*设置下拉框语言 */
function SetSelectLanguage(layui) {
    var $ = layui.$;
    //设置所有菜单显示文本
    var controlId;

    // 加载成功后，设置select的文本
    $("select").each(function () {
        var languageSelect = $(this);
        languageSelect.find('option').each(function () {
            controlId = $(this).attr("lay-event");
            if (controlId) {
                for (var key in lang) {
                    if (lang.hasOwnProperty(key)) {
                        if (key === controlId) {
                            $(this).text(lang[key]);
                        }
                    }
                }
            }
        });
    });
}
/*消息提示框 */
function ShowMessageByKey(layui, languageKey, iconIndex, timeOut, functions) {
    var text;
    for (var key in lang) {
        if (lang.hasOwnProperty(key)) {
            if (key === languageKey) {
                text = lang[key];
            }
        }
    }
    if (!text) {
        text = languageKey;
    }
    //layui.layer.msg(text, { title: lang.confirmTitle, icon: iconIndex, time: timeOut }, functions);
    layui.layer.msg(text, { icon: iconIndex, time: timeOut }, functions);
}
/*消息提示框（需要自己关闭） */
function AlertByKey(layui, languageKey, iconIndex) {
    var text;
    for (var key in lang) {
        if (lang.hasOwnProperty(key)) {
            if (key === languageKey) {
                text = lang[key];
            }
        }
    }
    if (!text) {
        text = languageKey;
    }
    layui.layer.alert(text, { icon: iconIndex });
}
/*打开窗体 */
function OpenPageByKey(layui, languageKey, type, width, height, url) {
    var text;
    for (var key in lang) {
        if (lang.hasOwnProperty(key)) {
            if (key === languageKey) {
                text = lang[key];
            }
        }
    }
    if (!text) {
        text = languageKey;
    }
    layui.layer.open({
        type: type,
        title: text,
        shadeClose: true,
        shade: 0.8,
        area: [width, height],
        content: url
    });
}
/*询问提示框 */
function ConfirmByKey(layui, languageKey, data, url, type, successKey, failKey, functions) {
    var $ = layui.$;
    var text;
    for (var key in lang) {
        if (lang.hasOwnProperty(key)) {
            if (key === languageKey) {
                text = lang[key];
            }
        }
    }
    if (!text) {
        text = languageKey;
    }
    layui.layer.confirm(text, {
        title: lang.confirmTitle,
        btn: [lang.confirmBtn, lang.closeBtn]
    }, function (index, layero) {
        $.ajax({
            type: type,
            url: url,
            data: data, // 你的formid
            async: false,
            error: function (request) {
                alert("Connection error");
            },
            success: function (data) {
                var item = eval(data);
                if (item.code === 0) {
                    ShowMessageByKey(layui, successKey, 6, 1000, functions);
                } else {
                    /**提示语弹框 */
                    ShowMessageByKey(layui, failKey, 0, 1000);
                }
            }
        });
    }, function (index) {
        //按钮【按钮二】的回调
    });
}

//通过key获取文本
function GetValueByKey(layui, languageKey) {
    var text;
    console.log(lang)
    for (var key in lang) {
       
        if (lang.hasOwnProperty(key)) {
            if (key === languageKey) {
                text = lang[key];
                console.log(text)
            }
        }
    }
    if (!text) {
        text = languageKey;
    }
    console.log(text,languageKey)
    return text;
}

function delTable(tabId) {
    const ul = document.getElementById("LAY_app_tabsheader");
    const lis = ul.getElementsByTagName("li");
    for (var i = 0; i < lis.length; i++) {
        var layId = lis[i].getAttribute("lay-id");
        if (tabId === layId) {
            lis[i].remove();
        }
    }
}

function previewImg(obj) {
    var img = new Image();
    img.src = obj.src;
    //var imgHtml = "<img src='" + obj.src + "' />";
    var imgHtml = "<div style='overflow:auto;display:flex;align-items:center;justify-content:center;max-width: 100%;max-height: 100%'>"
        + "<img style='max-width: 100%;max-height: 100%;position: absolute;top: 50%;transform: translateY(-50%);' src='" + obj.src + "' /></div>";
    //弹出层
    layer.open({
        type: 1,
        shade: [0.4, '#000000'],
        title: false, //不显示标题
        area: ['70%', '70%'],
        //maxWidth: '90px',
        //maxHeight: '90px',
        content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        cancel: function () {

        }
    });
}

//关闭选项卡
function delectTable(filter, menuId) {
    topElement.tabDelete(filter, menuId);
}